import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"

enum Api {
  page = "/infra-service/dataSource/pageDataSource",
  save = "/infra-service/dataSource/saveDataSource",
  listAllDataSource = "/infra-service/dataSource/listAllDataSource",
  listTable = "/infra-service/dataSource/listTable",
  edit = "/infra-service/dataSource/updateDataSource",
  get = "/infra-service/dataSource/getDataSource",
  delete = "/infra-service/dataSource/deleteDataSource",
  testConnection = "/infra-service/dataSource/testConnection",
  deleteBatch = "/infra-service/dataSource/batchDeleteDataSource",
}

/**
 * 查询数据源列表
 * @param params
 */
export const pageDataSource = (params) => {
  return defHttp.get({ url: Api.page, params })
}

/**
 * 查询全部数据源列表
 * @param params
 */
export const listAllDataSource = () => {
  return defHttp.get({ url: Api.listAllDataSource })
}
/**
 * 获取数据库中的表
 * @param params
 */
export const listDbTable = (params) => {
  return defHttp.get({ url: Api.listTable, params })
}

/**
 * 保存或者更新数据源
 * @param params
 */
export const saveOrUpdateDataSource = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

/**
 * 查询数据源详情
 * @param params
 */
export const getDataSourceById = (params) => {
  return defHttp.get({ url: Api.get, params })
}

/**
 * 删除数据源
 * @param params
 */
export const deleteDataSource = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

/**
 * 测试连接
 * @param params
 */
export const testConnection = (params) => {
  return defHttp.post({ url: Api.testConnection, params })
}

/**
 * 批量删除数据源
 * @param params
 */
export const batchDeleteDataSource = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "关联的生成表都将被删除, 是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
